<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	// module dir, empty if in core, = application if in dir
	function get_module($that)
	{
		$URI = uri_string();
		$controller = get_controller($that);
		$strpos = strpos($URI, $controller);
		$module = trim(substr($URI, 0, $strpos));
		if (substr($module, 0, 1) == '/') {
			$module = substr($module, 1);
		}
		if (substr($module, -1) == '/') {
			$module = substr($module, 0, -1);
		}
		return $module;
	}
	
	function get_module_from_uri($uri)
	{
		$controller = get_controller_from_uri($uri);
		if (is_empty($controller)) {
			$strpos = strlen($uri);
		} else {
			$strpos = strpos($uri, $controller);
		}
		$module = trim(substr($uri, 0, $strpos));
		if (substr($module, 0, 1) == '/') {
			$module = substr($module, 1);
		}
		if (substr($module, -1) == '/') {
			$module = substr($module, 0, -1);
		}
		return $module;
	}
	
	// full controller path (module/controller)
	function get_unit_controller($that)
	{
		return get_unit($that);
	}
	
	function get_unit_controller_from_uri($uri)
	{
		return get_unit_from_uri($uri);
	}
	
	// real controller name
	function get_controller($that)
	{
		return strtolower(get_class($that));
	}
	
	function get_controller_from_uri($uri)
	{ //XXX: find a better way
		$segs = uri_segment_array($uri);	

		if (strlen($segs[1])==3) {
			return strtolower($segs[2]);
		} else {
			return strtolower($segs[1]);
		}		
	}

	// unit name (PRF/Controller)
	function get_unit($that)
	{
		$unit = get_module($that);
		if (strlen(trim($unit))>0)
			$unit .= '/';
		$unit .= get_bare_unit($that);
		return $unit;
	}
	
	function get_unit_from_uri($uri) {
		$unit = get_module_from_uri($uri);
		if (strlen(trim($unit))>0)
			$unit .= '/';
		$unit .= get_bare_unit_from_uri($uri);
		return $unit;
	}
	
	function get_bare_unit($that)
	{
		return get_controller($that);
	}
	
	function get_bare_unit_from_uri($uri)
	{
		return get_controller_from_uri($uri);
	}
	
	function get_application($that)
	{
		$unit = get_unit($that);
		$prefix = "sys";
		
		if ((@substr($unit, 3, 1) == '/') || (@substr($unit, 3, 1) == ''))
			$prefix = substr($unit, 0, 3);

		return $prefix;
	}
	
	function get_application_from_uri($uri)
	{
		$unit = get_unit_from_uri($uri);
		$prefix = "sys";
		
		if ((@substr($unit, 3, 1) == '/') || (@substr($unit, 3, 1) == ''))
			$prefix = substr($unit, 0, 3);
		
		return $prefix;
	}
	
	function get_prefix($that)
	{
		return strtoupper(get_application($that));
	}
	
	function get_prefix_from_uri($uri)
	{
		return strtoupper(get_application_from_uri($uri));
	}

	function get_bare_function($that)
	{
		return $that->uri->segment(find_function_segment($that), 'index');
	}

	function get_bare_function_from_uri($uri)
	{
		$segs = uri_segment_array($uri);	
		if (isset($segs[find_function_segment_from_uri($uri)])) {
			return $segs[find_function_segment_from_uri($uri)];
		} else {
			return 'index';
		}
	}
	
	function get_function($that)
	{
		return get_unit($that).'/'.get_bare_function($that);
	}
	
	function get_function_from_uri($uri)
	{
		return get_unit_from_uri($uri).'/'.get_bare_function_from_uri($uri);
	}
	
	function find_controller_segment($that)
	{
		$segs = $that->uri->segment_array();
		$controller = get_controller($that);
		
		$result = false;
		
		foreach ($segs as $seg_idx => $segment)
		{
			if (strcasecmp($segment, $controller)==0) {
				$result = $seg_idx;
				break;
			}
		}
		return $result;
	}
	
	function find_controller_segment_from_uri($uri)
	{
		$segs = uri_segment_array($uri);	
		$controller = get_controller_from_uri($uri);

		$result = false;
			
		foreach ($segs as $seg_idx => $segment)
		{
			if (strcasecmp($segment, $controller)==0) {
				$result = $seg_idx;
				break;
			}
		}

		return $result;
	}
	
	function find_function_segment($that)
	{
		return (find_controller_segment($that)+1);
	}
	
	function find_function_segment_from_uri($uri)
	{
		return (find_controller_segment_from_uri($uri)+1);
	}
	
	function anchor_logout() {
		return anchor('authentication/logout', 'logout');
	}
	
	function is_empty($val) {
		$result = false;
			
		if (is_string($val)) {
			$result = (strlen($val)==0);
		} else {
			$result = (count($val)==0);
		}
		
		return $result;
	}
	
	function is_null_or_false($val) {
		return (is_null($val) || ($val===FALSE));
	}
	
    function in_arrayi($needle, $haystack) {
        return in_array(strtolower($needle), array_map('strtolower', $haystack));
    }
	
	function case_insensitive_search($needle, $haystack) {
		$result = false;
        foreach ($haystack as $hi => $hv) {
			if (strcasecmp($needle, $hv)==0) {
				$result = $hv;
				break;
			}
		}
		return $result;
    }
	
	function generate_csv($fields, $data, $delim = ",", $newline = "\n", $enclosure = '"', $header = "")
	{	
		$out = '';

		$out .= $header . $newline;
		
		// First generate the headings from the table column names
		foreach ($fields as $name)
		{
			$out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $name).$enclosure.$delim;
		}
		
		$out = rtrim($out);
		$out .= $newline;
		
		// Next blast through the result array and build out the rows
		foreach ($data as $row)
		{
			foreach ($row as $item)
			{
				$out .= $enclosure.str_replace($enclosure, $enclosure.$enclosure, $item).$enclosure.$delim;			
			}
			$out = rtrim($out);
			$out .= $newline;
		}

		return $out;
	}
	
	function generate_xml($data, $params = array())
	{	
		// Set our default values
		foreach (array('root' => 'root', 'element' => 'element', 'newline' => "\n", 'tab' => "\t") as $key => $val)
		{
			if ( ! isset($params[$key]))
			{
				$params[$key] = $val;
			}
		}
		
		// Create variables for convenience
		extract($params);
			
		// Load the xml helper
		$CI =& get_instance();
		$CI->load->helper('xml');

		// Generate the result
		$xml = "<{$root}>".$newline;
		foreach ($data as $row)
		{
			$xml .= $tab."<{$element}>".$newline;
			
			foreach ($row as $key => $val)
			{
				$xml .= $tab.$tab."<{$key}>".xml_convert($val)."</{$key}>".$newline;
			}
			$xml .= $tab."</{$element}>".$newline;
		}
		$xml .= "</$root>".$newline;
		
		return $xml;
	}
	
	function uri_segment_array($uri) {
		if (substr($uri, 0, 1)=='/') $uri = substr($uri, 1); // remove front trailing '/'
		if (substr($uri, -1)=='/') $uri = substr($uri, 0, -1); // remove end trailing '/'
		
		$_result = explode("/", $uri);
		
		$result = array();
		foreach ($_result as $idx => $value) {
			$result[$idx+1] = $value;
		}
		return $result;
	}
	